/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utilidades;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author alrojas
 */
public class SpinnerEditor extends AbstractCellEditor implements TableCellEditor{
 
    final JSpinner spinner = new JSpinner();
  
  public SpinnerEditor(int maximum) {
    spinner.setModel(new SpinnerListModel(generateInterval(maximum)));
    spinner.setValue("1");
  }

  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
      int row, int column) {
    spinner.setValue(value);
    return spinner;
  }

  public boolean isCellEditable(EventObject evt) {
    if (evt instanceof MouseEvent) {
      return ((MouseEvent) evt).getClickCount() >= 1;
    }
    return true;
  }

  public Object getCellEditorValue() {
    return spinner.getValue();
  }
  
  
  // La siguiente función tambien es un asco, iuu!
  public String[] generateInterval(int maximum){
      String [] values = new String[maximum];
      for(int i = 1; i < maximum; i++){
          values[i] = String.valueOf(i);
      }
      
      return values;
  }
}
